<?php

namespace App\Model\Purchase;

use App\Core\BaseModel;
use App\Core\Exception\DefaultException;
use App\Model\Member\MemberModel;
use App\Model\Member\MemberProfitScoreModel;
use App\Model\Member\MemberScoreModel;
use App\Util\UserIdentityHelper;
use EasySwoole\Component\Di;
use App\Model\Member\VipModel;
use EasySwoole\Redis\Redis;
/**
 * CREATE TABLE `ss_purchase_order` (
 * `id` char(24) NOT NULL COMMENT '订单id',
 * `goods_name` varchar(255) NOT NULL COMMENT '商品名称',
 * `goods_img` varchar(255) NOT NULL COMMENT '商品图片',
 * `goods_price` decimal(10,2) NOT NULL DEFAULT '0.00' COMMENT '商品单价',
 * `goods_quantity` int(11) NOT NULL DEFAULT '0' COMMENT '商品数量',
 * `order_amount` decimal(10,2) NOT NULL COMMENT '订单实付金额',
 * `order_state` char(10) NOT NULL DEFAULT 'UNSHIPPING' COMMENT '订单状态 UNSHIPPING:待发货;SHIPPED:已发货;RECEIPT:确认收货;AUDITING:退货审核中;AUDITED:退货审核通过;AFTERSALES:退货售后;RETURNED:已退货',
 * `order_time` datetime DEFAULT NULL COMMENT '下单时间',
 * `shipped_time` datetime NOT NULL COMMENT '发货时间',
 * `receipt_time` datetime DEFAULT NULL COMMENT '收货时间',
 * `created_at` datetime NOT NULL DEFAULT '1970-01-01 00:00:00' COMMENT '创建时间',
 * `created_by` varchar(32) NOT NULL DEFAULT '' COMMENT '创建人员账号',
 * `updated_at` datetime NOT NULL DEFAULT '1970-01-01 00:00:00' ON UPDATE CURRENT_TIMESTAMP COMMENT '最后更新时间',
 * `updated_by` varchar(32) NOT NULL DEFAULT '' COMMENT '最后更新人员账号',
 * `is_delete` tinyint(1) NOT NULL DEFAULT '0' COMMENT '是否删除[0:未删除，1：已删除]',
 * `deleted_at` datetime NOT NULL DEFAULT '1970-01-01 00:00:00' COMMENT '删除时间',
 * PRIMARY KEY (`id`) USING BTREE,
 * KEY `created_at` (`created_at`) USING BTREE
 * ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='订单';
*/
/**
 * Class PurchaseModel
 * @property string id
 * @property string goods_name
 * @property string goods_img
 * @property string goods_price
 * @property string goods_quantity
 * @property string order_amount
 * @property string order_state
 * @property string order_time
 * @property string shipped_time
 * @property string receipt_time
 * @property string created_at
 * @property string created_by
 * @property string updated_at
 * @property string updated_by
 * @property string is_delete
 * @property string deleted_at
 */
class PurchaseModel extends BaseModel
{

    protected $tableName = 'ss_purchase_order';

    protected $casts = [
        'id'            => 'string',
        'goods_name'    => 'string',
        'goods_img'     => 'string',
        'goods_price'   => 'float',
        'goods_quantity' => 'int',
        'order_amount'  => 'float',
        'order_state'   => 'string',
        'order_time'    => 'string',
        'shipped_time'  => 'string',
        'receipt_time'  => 'string',
        'created_at'    => 'string',
        'created_by'    => 'string',
        'updated_at'    => 'string',
        'updated_by'    => 'string',
        'is_delete'     => 'int',
        'deleted_at'    => 'string',
    ];
    //订单状态 UNSHIPPING:待发货;SHIPPED:已发货;RECEIPT:确认收货;AUDITING:退货审核中;AUDITED:退货审核通过;AFTERSALES:退货售后;RETURNED:已退货
    const UNSHIPPING = 'UNSHIPPING';//待发货
    const SHIPPED    = 'SHIPPED';//已发货
    const RECEIPT    = 'RECEIPT';//确认收货
    /**
     * 下单后X小时
     */
    const SHIPPEDTIME = [12, 28]; //发货时间
    const RECEIPTTIME = [72, 120]; //收货时间
    const ORDERLIMIT = 100;//订单限制默认提成
    public static function getOrderStateDict($key = null)
    {
        return self::getMap([
            self::UNSHIPPING => '待发货',
            self::SHIPPED    => '已发货',
            self::RECEIPT    => '确认收货',
        ], $key);
    }
    protected static function onAfterUpdate($model, $res)
    {
        self::delCache('purchaseCache:');
    }

    protected static function onAfterInsert($model, $res)
    {
        self::delCache('purchaseCache:');
    }

}
